<template>
  <div class="user-page page-main">
    <search-form :model="searchParams" @search="search">
      <a-form-item label="用户名" name="username">
        <a-input v-model:value="searchParams.username" placeholder="请输入" />
      </a-form-item>
    </search-form>
    <div class="table-toolbar">
      <add-button @click="add()" />
    </div>
    <g-table
      ref="tableRef"
      :columns="columns"
      :api="adminList"
      :params="searchParams"
    />

    <UserDetail
      v-model:visible="visible"
      :data="activeRow"
      @change="search(false)"
    />
  </div>
</template>

<script setup lang="tsx">
import { adminDel, adminList } from "@/api/user";
import StatusBadge from "@/components/StatusBadge.vue";
import type { IUser } from "@/types/user";
import UserDetail from "./detail.vue";

const searchParams = reactive({
  username: "",
  order: "asc",
});

let visible = $ref(false);

const { tableRef, columns, search } = useTable<IUser>([
  { title: "用户ID", dataIndex: "id", width: 80 },
  { title: "用户名", dataIndex: "account", width: 100 },
  { title: "是否接收短信", dataIndex: "deptName", width: 160 },
  { title: "最后登录IP", dataIndex: "lastIp", width: 160 },
  { title: "手机号", dataIndex: "phone", width: 140 },
  {
    title: "状态",
    dataIndex: "status",
    width: 80,
    customRender: ({ record }) => (
      <StatusBadge status={record.status ? 1 : 0} />
    ),
  },
  { title: "最后登录时间", dataIndex: "lastTime", width: 180 },
  {
    title: "操作",
    dataIndex: "op",
    width: 120,
    fixed: "right",
    customRender: ({ record }) => (
      <>
        <table-button onClick={() => add(record)}>编辑</table-button>
        <confirm-button danger onClick={() => remove(record)}>
          删除
        </confirm-button>
      </>
    ),
  },
]);

let activeRow = $ref<Partial<IUser>>({});
function add(row: Partial<IUser> = {}) {
  activeRow = { ...row };
  visible = true;
}

function remove({ id }: IUser) {
  adminDel(id as number).then(() => {
    AntMessage.success("已删除");
    search(false);
  });
}
</script>

<style lang="less"></style>
